#include "../inc/Ui/TwitterForm.h"
#include "../inc/Ui/TwitterLoginForm.h"
#include"../inc/Ui/FormManager.h"
#include <FApp.h>

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Social;
using namespace Osp::Social::Services;
using namespace Osp::Media;
using namespace Osp::Graphics;

int TwitterForm::FormId = -1;
TwitterForm::TwitterForm(void) {
}

TwitterForm::~TwitterForm(void) {
}

bool TwitterForm::Initialize() {
	Form::Construct(L"Twitter_FormMain");

	return true;
}

result TwitterForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	Application *pApp = Application::GetInstance();

	String temp = L"";
	/*
	 * Option Menu
	 */
	pOptionMenu = new OptionMenu();
	pOptionMenu->Construct();

	pApp->GetAppResource()->GetString("IDS_FORMMAIN_SIGNOUT", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_SIGNOUT);

	pApp->GetAppResource()->GetString("IDS_FORMMAIN_MYPROFILE", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_MYPROFILE);

	pApp->GetAppResource()->GetString("IDS_FORMMAIN_FOLLOWING", temp);
	pOptionMenu->AddItem(temp, ID_OPTIONMENU_FOLLOWING);

	SetOptionkeyActionId(ID_OPTIONMENU);
	AddOptionkeyActionListener(*this);
	pOptionMenu->AddActionEventListener(*this);
	/*
	 * CustomList
	 *
	 */
	Rectangle clientRect;
	clientRect=GetClientAreaBounds();
	Rectangle listRect;
	pCustomList=new CustomList();
	pCustomList=static_cast<CustomList*>(GetControl("IDC_CUSTOMLIST1"));

	listRect=pCustomList->GetBounds();
	pListItemFormat=new CustomListItemFormat();
	pListItemFormat->Construct();
	pListItemFormat->AddElement(ID_ELEMENT_ACTIVITY, Rectangle(10, 0, listRect.width- 20, 150), 32, pCustomList->GetTextColorOfEmptyList(),pCustomList->GetTextColorOfEmptyList());
	pListItemFormat->AddElement(ID_ELEMENT_NAME, Rectangle(10, 145, listRect.width - 20, 40), 28, Color::COLOR_CYAN,Color::COLOR_GREY);
	pButton_addmystatus = static_cast<Button *>(GetControl("IDC_BUTTON_ADDMYSTATUS"));
	if (pButton_addmystatus)
	{
		pButton_addmystatus->SetActionId(ID_BUTTON_ADDMYSTATUS);
		pButton_addmystatus->AddActionEventListener(*this);
	}
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	pEditFieldStatusText=static_cast<EditField*>(GetControl("IDC_EDITFIELD_ADDMYSTATUS"));

	pButtonNext = static_cast<Button *>(GetControl("IDC_BUTTON_NEXT"));
	pButtonNext->SetActionId(ID_BUTTON_NEXT);
	pButtonNext->AddActionEventListener(*this);
	pButtonHome=static_cast<Button*>(GetControl("IDC_BUTTON_HOME"));
	pButtonHome->SetActionId(ID_BUTTON_HOME);
	pButtonHome->AddActionEventListener(*this);


	  // Create Bitmaps
	    Bitmap* pBitmapNext  = GetBitmapN("next.jpg");
	    Bitmap* pBitmapHome = GetBitmapN("home.jpg");
	    Bitmap* pBitmapNextPress  = GetBitmapN("nextpress.jpg");
	    Bitmap* pBitmapHomePress = GetBitmapN("homepress.jpg");

	    // Set Bitmaps to the bitmap Button.
	    AppLog("Vao button add image");
	    pButtonNext->SetNormalBackgroundBitmap(*pBitmapNext);
	    pButtonNext->SetPressedBackgroundBitmap(*pBitmapNextPress );
	    pButtonHome->SetNormalBackgroundBitmap(*pBitmapHome);
	    pButtonHome->SetPressedBackgroundBitmap(*pBitmapHomePress);

	    AppLog("KetThus");
	    // Deallocate Bitmaps
	    delete pBitmapNext;
	    delete pBitmapHome;

	return r;
}

result TwitterForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}
void TwitterForm::AddActivities(IList* pActivityList) {
	pCustomList->RemoveAllItems();
	AppLog("Form1::AddActivities");
	SnsActivity* pActivity = null;
	IEnumerator* pEnum = null;
	String snsUserName;
	String content;
	DateTime timeAct;
	CustomListItem* pItem = null;
	int itemHeight = 195;
	AppLog("My User Name:%S", myUserName.GetPointer());
	if (pActivityList == null) {
		AppLog("pActivityList is null.");
		ThongBaoLoi("Việc lấy dữ liệu bạn bè đã thất bại");
	} else {
		pEnum = pActivityList->GetEnumeratorN();
		while (pEnum->MoveNext() != E_OUT_OF_RANGE) {
			pActivity = static_cast<SnsActivity*> (pEnum->GetCurrent());
			pActivity->GetValue(SNS_ACTIVITY_PID_OWNER_NAME, snsUserName);
			pActivity->GetValue(SNS_ACTIVITY_PID_CONTENT, content);
			pActivity->GetValue(SNS_ACTIVITY_PID_ACT_TIME, timeAct);
			if (snsUserName.Equals(this->myUserName) == true) {
				snsUserName = "me";
			}
			snsUserName.Append(",");
			snsUserName.Append(timeAct.ToString());

			pItem = new CustomListItem();
			pItem->Construct(itemHeight);
			pItem->SetItemFormat(*pListItemFormat);
			pItem->SetElement(ID_ELEMENT_NAME, snsUserName);
			pItem->SetElement(ID_ELEMENT_ACTIVITY, content);
			pCustomList->AddItem(*pItem);
		}
		delete pEnum;
	}

	pCustomList->RequestRedraw(true);
	SetStateToButton(true, true, true);
	pButton_addmystatus->SetEnabled(true);

}
void TwitterForm::SetStateToButton(bool home, bool next, bool statOption) {
	pButtonHome->SetEnabled(home);
	pButtonNext->SetEnabled(next);
	IsOpenedOptionMenu = statOption;
}
void TwitterForm::OnActionPerformed(const Osp::Ui::Control& source,
		int actionId) {
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	Form *pForm = null;
	switch (actionId) {
	case ID_BUTTON_NEXT: {
		AppLog("Vao button next");
		SetStateToButton(false, false, false);
		bool kq = pSnsGatewayTwitterForm->RequestMoreActivities();
		if (kq == false) {
			AppLog("Ket qua=false");
			SetStateToButton(true, true, true);
			pButton_addmystatus->SetEnabled(true);
		}
	}
		break;

		break;
	case ID_BUTTON_HOME: {
		AppLog("Vao button home");
		SetStateToButton(false, false, false);
		bool kq = pSnsGatewayTwitterForm->RequestHomeActivies();
		if (kq == false) {
			AppLog("Ket qua=false");
			SetStateToButton(true, true, true);
			pButton_addmystatus->SetEnabled(true);
		}
	}
		break;
	case ID_OPTIONMENU_MYPROFILE:

		pFormManager->SetCurrentForm(MyProfileForm::GetFormId());
		pSnsGatewayTwitterForm->RequestMyProfile();
		break;
	case ID_OPTIONMENU: {
		AppLog("Vao option mennu");
		if (IsOpenedOptionMenu == true) {
			pOptionMenu->SetShowState(true);
			pOptionMenu->Show();
		}
	}
		break;
	case ID_OPTIONMENU_SIGNOUT:

		pFormManager->SetCurrentForm(Form1::GetFormId());

		pSnsGatewayTwitterForm->RemoveTwitterAuthResult();
		pCustomList->RemoveAllItems();
		AppLog("Sign out");
		break;
	case ID_OPTIONMENU_FOLLOWING: {
		AppLog("Vao following");
		pSnsGatewayTwitterForm->RequestFollowingList();
		pCustomList->RemoveAllItems();
		pFormManager->SetCurrentForm(MyFollowingForm::GetFormId());
	}
		break;

		break;
	case ID_SOFTKEY_BACK: {
		TroVeTrangChu();
	}
		break;
	case ID_BUTTON_ADDMYSTATUS: {
		ThemStatus();
	}
		break;
	default:
		break;
	}
}
void TwitterForm::ThemStatus() {
	AppLog("Vao trong add My status");

	String myStatusText = pEditFieldStatusText->GetText();
	if (myStatusText.GetLength() > 0 && myStatusText.GetLength()
			< MAX_STATUS_LEN) {
		pSnsGatewayTwitterForm->UpdateMyStatusText(myStatusText);
		pButton_addmystatus->SetEnabled(false);

	} else {
		ThongBaoLoi("Ban chưa nhập status");
		return;
	}

}
void TwitterForm::ThongBaoLoi(String value) {
	MessageBox msg;
	msg.Construct("Chào bạn ", value, MSGBOX_STYLE_OK, 3000);
	int i = 0;
	msg.ShowAndWait(i);

}
void TwitterForm::SetMyUseName(String userName) {
	this->myUserName = userName;
}
MySnsGatewayManager* TwitterForm::GetSnsGatewayTwitterForm() {
	return pSnsGatewayTwitterForm;
}
void TwitterForm::SetSnsGateWayTwitterForm(
		MySnsGatewayManager*pSnsGatewayManager) {
	pSnsGatewayTwitterForm = pSnsGatewayManager;
}
void TwitterForm::DisplayResultUpdateStatusText() {
	pEditFieldStatusText->SetText("");
	pCustomList->RemoveAllItems();
	pSnsGatewayTwitterForm->RequestActivities();
	pEditFieldStatusText->RequestRedraw(true);

}
Bitmap*
TwitterForm::GetBitmapN(const Osp::Base::String& name) {
	Bitmap* pBitmap = null;
	Image* pImage = new Image();

	String fullname(L"/Res/imgPlace/");
	fullname.Append(name);
	AppLog("Get bitmap:%S", fullname.GetPointer());
	pImage->Construct();

	if(fullname.EndsWith(L"jpg"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"bmp"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	else if(fullname.EndsWith(L"png"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_ARGB8888);
	}
	else if (fullname.EndsWith(L"gif"))
	{
		pBitmap = pImage->DecodeN(fullname, BITMAP_PIXEL_FORMAT_RGB565);
	}
	delete pImage;

	return pBitmap;

}

void TwitterForm::TroVeTrangChu() {
	FormManager *pFormManager = null;
	Form *pTwitterLoginForm = null;
	FormManager::GetInstance(pFormManager);

	result r = pFormManager->GetForm(Form1::GetFormId(), pTwitterLoginForm);
	if (IsFailed(r)) {
		return;
	}
	static_cast<Form1*> (pTwitterLoginForm)->ExcuteBack();
}
